<?php
/**
 * 首页
 * User: tsang
 * Date: 2017/9/14
 * Time: 20:11
 */
namespace app\admin\controller;
use app\admin\Controller\Export;
use Doctrine\Common\Cache\MemcacheCache;
use think\Session;
use think\Request;
use think\cache\driver\Redis;
class Prizes extends Common
{
    public $enterprise_num;
    public $project_num;

    /*
    *构造函数
    */
    function __construct(){
        parent::__construct();
    }

    /**
    *奖品列表
    */
    public function index(){
        $aid = input('activity');
        $list = db('activity_prizes')->where(['aid'=>$aid])->paginate(10);
        $config_update = IMG_PATH;
        $this->assign('list',$list);
        $this->assign('aid',$aid);
        $this->assign('config_update',$config_update);
        return $this->fetch();
    }

    /**
    *奖品添加
    */
    public function add(){
        $activity_id = input('activity_id');
        $id = input('id');
        $activity_prizes_find = db('activity_prizes')->where(['id'=>$id])->find();
        $config_update = IMG_PATH;

        //查询所有奖品
        $activity_list = db('activity_prizes')->where(['aid'=>$activity_id])->select();

        if(request()->isAjax()) {
            $number_prizes = input('number_prizes');
            $prize_name = input('prize_name');
            $type = input('type');
            $money=input('prizes_money');
            $stock=input('stock');
            
            $file=Request::instance()->file('prizes_image');

            if($file){
                $info = $file->move(ROOT_PATH . 'public/static/uploads');

                if($info){
                    $data['prize_img']= "/".str_replace('\\',"/",$info->getSaveName());
                }
            }

            $data['aid'] = $activity_id;
            $data['prize_name'] = $prize_name;
            $data['money'] = $money;
            $data['stock'] = $stock;
            if(!empty($type)){
                $data['type'] = $type;
            }

            $probability=[];
            foreach ($activity_list as $key => $value) {
                if($value['id'] != $id){
                    $probability[$value['id']] = $value['probability'];
                }
            }
            $probability = array_sum($probability);
            $probability += $number_prizes;

            if($probability > 100)return $this->error('总奖池已超过100%，请注意设置');

            $data['probability'] = $number_prizes;

            if(empty($id)){
                //$activity_prizes_bool = db('activity_prizes')->where(['prize_name'=>$prize_name])->find();
                //if(!empty($activity_prizes_bool))return $this->error('该奖品已添加过了');
                db('activity_prizes')->insert($data);
            }else{
                db('activity_prizes')->where(['id'=>$id])->update($data);
            }

            $this->success('操作成功',url('Prizes/index',array('activity'=>$activity_id)));
        }

        $this->assign('activity_id',$activity_id);
        $this->assign('activity_prizes_find',$activity_prizes_find);
        $this->assign('config_update',$config_update);
        $this->assign('activity_list',$activity_list);
        $this->assign('id',$id);
        return $this->fetch();
    }

    /**
    *中奖人列表
    */
    public function prizes_list(){
		if(input('export')){
			$this->export();exit;
		}
        $pageParam = ['query' =>[]];

        $mobile = input('mobile');
        if($mobile){
            $where['m.mobile'] = array('like','%'.$mobile.'%');
        }
        $pageParam['query']['mobile'] = $mobile;

        $title = input('title');
        if($title){
            $where['ay.title'] = array('like','%'.$title.'%');
        }
        $pageParam['query']['title'] = $title;

        $list = db('lottery_record')->alias('lr')
            ->where(['is_winning'=>'1'])
            ->join('activity ay','ay.id=lr.aid')
            ->join('member m','m.id=lr.uid')
            ->field('m.mobile,m.nickname,m.headimage,ay.title,lr.*')
            ->order('lr.id desc')
            ->paginate(15,false,$pageParam);

        $this->assign('mobile',$mobile);
        $this->assign('title',$title);
        $this->assign('data', $list);
        return $this->fetch();
    }
	
	/*
	*	导出excel记录
	*/
	public function export()
	{
		ini_set('memory_limit','1024M');
		set_time_limit ( 0 );

		$headArr = array("中奖人电话","领取时间","奖品名称","收件人","收件人电话","收件人地址");
        $list = db('activity_prizes_list')->alias('apl')
        ->join('member m', 'm.id=apl.uid', 'INNER')
        ->join('activity_prizes_records apr', 'apl.id = apr.aplid', 'LEFT')
        ->join('activity_prizes ap', 'apl.acid=ap.id', 'INNER')
        ->join('activity a', 'a.id=ap.aid', 'INNER')
        ->where('ap.type',2)
        ->field('apl.*,m.mobile as umobile,apr.mobile,apr.path,apr.name,ap.prize_name')
        ->select();

		$data=array();
		foreach($list as $k => $v){
			$data[$k][] = $v['umobile'];
			$data[$k][] = date("Y-m-d H:i",$v['receive_time']);
			$data[$k][] = $v['prize_name'];
            $data[$k][] = $v['name'];
            $data[$k][] = $v['mobile'];
            $data[$k][] = $v['path'];
		}
		$filename='中奖记录-'.date("Y-m-d",time());
		(new Export())->ImportExcel($filename,$headArr,$data);
	}
}